package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.repository.entity.Project;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 项目表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-06-20
 */
@Mapper
public interface ProjectMapper extends BaseMapper<Project> {

    @Select("<script>" +
            "SELECT M.* from " +
            "(SELECT a.*,b.customer_name,c.username FROM dragon_project a " +
            "LEFT JOIN dragon_customer b ON a.customer_id = b.id " +
            "LEFT JOIN dragon_admin c ON a.admin_id = c.id " +
            "LEFT JOIN dragon_admin_department d ON d.admin_id = a.admin_id " +
            "LEFT JOIN dragon_team e ON e.project_id = a.id "+
            "<if test='search != null and search != \"\"'> "+
            "and (b.id like CONCAT('%',#{search},'%')  )" +
            "</if>" +
            " )M WHERE 1=1 " +
            "<if test='search != null and search != \"\"'> "+
            "and (M.name like CONCAT('%',#{search},'%')  )" +
            "</if>" +
            "<if test='departmentIds != null and departmentIds.size!=0'>" +
            "AND M.department_id in" +
            "<foreach collection = 'departmentIds' item = 'item' open = '(' close = ')' separator = ','>" +
            "#{item}" +
            "</foreach>" +
            "</if>" +
            "<if test='aId != null and aId != \"\"'>" +
            "AND M.admin_id = #{aId} "+
            "</if>" +
            "</script>")
    Page<Project> selectProject(Page<Project> projectPage,String search,List<String> departmentIds,Integer aId);

    /**
     * 根据 客户id 查询 对应项目
     * @param customerId
     * @return
     */
    @Select("SELECT * FROM dragon_project a " +
            "LEFT JOIN dragon_customer b on a.customer_id = b.id " +
            "WHERE b.id = #{customerId} ")
    List<Project> selectByCustomerId(Integer customerId);

    @Select("select a.*,b.customer_name from dragon_project a LEFT JOIN dragon_customer b on a.customer_id = b.id WHERE a.id = #{id}")
    Page< Project> viewId(Page<Project> page,Integer id);
}
